package com.wms.model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import com.wms.helper.Database;



/**
 * Provides data methods necessary for the Admin to function.
 * @author Suki
 */
public class AdminModel {
	
	/**
	 * Simple constructor -- allows the Internet to be accessed on the main thread.
	 */

	/**
	 * 
	 * @param username
	 * @throws SQLException
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws ClassNotFoundException
	 */
	public void activateUser(String username) throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException{
		Connection conn = Database.getWriteConnection();		
		
		PreparedStatement statement = conn.prepareStatement("UPDATE User SET status='active' WHERE username=?");
		statement.setString(1, username);
		
		statement.executeUpdate();
		
		statement.close();
		conn.close();
	}
	
	/**
	 * 
	 * @param username
	 * @throws SQLException
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws ClassNotFoundException
	 */
	public void setAdmin(String username) throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException{
		Connection conn = Database.getWriteConnection();		
		
		PreparedStatement statement = conn.prepareStatement("UPDATE User SET role='admin' WHERE username=?");
		statement.setString(1, username);
		
		statement.executeUpdate();
		
		statement.close();
		conn.close();
	}
	
	/**
	 * 
	 * @param username
	 * @throws SQLException
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws ClassNotFoundException
	 */
	public void removeUser(String username) throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
		Connection conn = Database.getWriteConnection();
		
		PreparedStatement statement = conn.prepareStatement("DELETE FROM User WHERE username=?");
		statement.setString(1, username);
		
		statement.executeUpdate();
		
		statement.close();
		conn.close();
	}
}
